import { defineStore } from 'pinia';
import { ref } from 'vue';
import api from '@/http/api';

export const useOrganizationStore = defineStore('organization', () => {
  const regions = ref([]);
  const units = ref([]);
  const entps = ref([]);
  const organizations = ref([]);
  const regionLoading = ref(false);
  const unitLoading = ref(false);
  const entpLoading = ref(false);
  const loading = ref(false);

  const setRegion = (tree) => {
    regions.value = tree && tree.children || [];
  };

  const getRegion = async() => {
    if (regions.value && regions.value.length > 0) {
      return regions.value;
    } else {
      return await loadRegion();
    }
  };

  const loadRegion = async() => {
    if (regions.value && regions.value.length > 0) {
      regionLoading.value = false;
      return regions.value;
    }

    if (regionLoading.value) {
      await sleep(100);
      return await loadRegion();
    }

    regionLoading.value = true;
    if (regions.value && regions.value.length) {
      regionLoading.value = false;
      return regions.value;
    } else {
      const res = await api.post('/business/region/tree').catch(() => {
        regionLoading.value = false;
      });
      regionLoading.value = false;
      if (res.code === '1') {
        setRegion(res.data);
        return regions.value;
      }
    }
  };

  const setUnit = (tree) => {
    units.value = tree && tree.children || [];
  };

  const getUnit = async() => {
    if (units.value && units.value.length > 0) {
      return units.value;
    } else {
      return await loadUnit();
    }
  };

  const loadUnit = async() => {
    if (units.value && units.value.length > 0) {
      unitLoading.value = false;
      return units.value;
    }

    if (unitLoading.value) {
      await sleep(100);
      return await loadUnit();
    }

    unitLoading.value = true;
    if (units.value && units.value.length) {
      unitLoading.value = false;
      return units.value;
    } else {
      const res = await api.post('/business/unit/tree').catch(() => {
        unitLoading.value = false;
      });
      unitLoading.value = false;
      if (res.code === '1') {
        setUnit(res.data);
        return units.value;
      }
    }
  };

  const setEntp = (tree) => {
    entps.value = tree && tree.children || [];
  };

  const getEntp = async() => {
    if (entps.value && entps.value.length > 0) {
      return entps.value;
    } else {
      return await loadEntp();
    }
  };

  const loadEntp = async() => {
    if (entps.value && entps.value.length > 0) {
      entpLoading.value = false;
      return entps.value;
    }

    if (entpLoading.value) {
      await sleep(100);
      return await loadEntp();
    }

    entpLoading.value = true;
    if (entps.value && entps.value.length) {
      entpLoading.value = false;
      return entps.value;
    } else {
      const res = await api.post('/business/ent/tree').catch(() => {
        entpLoading.value = false;
      });
      entpLoading.value = false;
      if (res.code === '1') {
        setEntp(res.data);
        return entps.value;
      }
    }
  };

  const setOrganization = (tree) => {
    organizations.value = tree && tree.children || [];
  };

  const getOrganization = async() => {
    if (organizations.value && organizations.value.length > 0) {
      return organizations.value;
    } else {
      return await loadOrganization();
    }
  };

  const loadOrganization = async() => {
    if (organizations.value && organizations.value.length > 0) {
      loading.value = false;
      return organizations.value;
    }

    if (loading.value) {
      await sleep(100);
      return await loadOrganization();
    }

    loading.value = true;
    if (organizations.value && organizations.value.length) {
      loading.value = false;
      return organizations.value;
    } else {
      const res = await api.post('/business/organization/tree').catch(() => {
        loading.value = false;
      });
      loading.value = false;
      if (res.code === '1') {
        setOrganization(res.data);
        return organizations.value;
      }
    }
  };

  const clearOrganization = async() => {
    regions.value = [];
    units.value = [];
    entps.value = [];
    organizations.value = [];
  };

  const sleep = async (d) => {
    return new Promise((resolve) => setTimeout(resolve, d));
  };

  return {
    getRegion,
    getUnit,
    getEntp,
    getOrganization,
    clearOrganization
  };
});
